<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class DAO_Finder extends ORM_Finder {
    
    static function result($sql, $model='DB_USER',$arr=null) {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        $rs = $conn->query($sql, $arr);
        $return = array();
        while ($data = $rs->fetch()) {
            $m = new $model;
            foreach ($data as $key => $d) {
                call_user_func(array($m, 'set'.$key), $d);
            }
            $m->setNew(false);
            $return[] = $m;
        }
        return $return;
    }

    static function querySQl($sql, $model='DB_USER') {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        $arr    = array();
        $rs = $conn->query($sql, $arr);
    }
    
    static function exec($sql, $model='DB_USER', $arr=null) {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        $rs = $conn->query($sql, $arr);
        return $rs;
    }


    static function getRows($sql, $model='DB_USER') {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        $arr    = array();
        $rs = $conn->query($sql, $arr);
        return $rs->fetchColumn;
    }
    
    static function daoAll($model, $conditions = null, $fields = '*', $order = null, $limit = null , $file_cache=false) {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        if (!empty($conditions)) {
            $where = "";
            if (is_array($conditions)) {
                foreach ($conditions as $field => $value) {
                    $where[] = " $field = ?";
                    $values[] = $value;
                }
            } else {
                $where = $conditions;
                $values = array();
            }
        } else {
            $where = '';
            $values = array();
        }
        if(!empty($where)) {
            $where  =  "WHERE". implode(' and ', $where);
        }
        is_array($fields) ? $fields = join(',', $fields) : $fields;
        if (NUll != $order) {
            $order = 'ORDER BY '.$order;
        }
        if (NULL != $limit) {
            $limit = 'LIMIT '.$limit;
        }
        $table = call_user_func(array($model, 'getTableName'), $conditions);
        $sql    = "SELECT $fields FROM {#prefix#}".$table." $where $order $limit";
        
        $rs = $conn->query($sql, $values);
        $return = array();
        while ($data = $rs->fetch()) {
            $m = new $model;
            foreach ($data as $key => $d) {
                call_user_func(array($m, 'set'.$key), $d);
            }
            $m->setNew(false);
            $return[] = $m;
        }
        return $return;
    }
    static function daoOne($model, $conditions = null, $fields = '*', $order = null, $file_cache=false) {
        $r = self::daoAll($model, $conditions, $fields, $order, 1, $file_cache=false);
        return isset($r[0])?$r[0]:null;
    }
}
?>
